<template>
	<view class="forum-container">
		<!-- 顶部搜索栏 -->
		<view class="search-bar">
			<view class="search-input">
				<text class="iconfont icon-search">🔍</text>
				<input type="text" v-model="searchText" placeholder="搜索感兴趣的内容" />
			</view>
			<view class="post-btn" @click="createPost">
				<text class="iconfont icon-edit">✏️</text>
			</view>
		</view>

		<!-- 分类标签 -->
		<scroll-view class="category-scroll" scroll-x>
			<view class="category-list">
				<view 
					v-for="(category, index) in categories" 
					:key="index"
					:class="['category-item', { active: currentCategory === category.id }]"
					@click="selectCategory(category.id)"
				>
					{{ category.name }}
				</view>
			</view>
		</scroll-view>

		<!-- 帖子列表 -->
		<scroll-view 
			class="posts-list" 
			scroll-y 
			refresher-enabled
			@refresherrefresh="onRefresh"
			@scrolltolower="loadMore"
		>
			<view 
				class="post-item" 
				v-for="(post, index) in posts" 
				:key="index"
				@click="viewPostDetail(post.id)"
			>
				<view class="post-header">
					<image class="avatar" :src="post.author.avatar" mode="aspectFill"></image>
					<view class="author-info">
						<text class="author-name">{{ post.author.name }}</text>
						<text class="post-time">{{ post.createTime }}</text>
					</view>
				</view>
				<view class="post-content">
					<text class="post-title">{{ post.title }}</text>
					<text class="post-text">{{ post.content }}</text>
					<view class="post-images" v-if="post.images && post.images.length">
						<image 
							v-for="(img, imgIndex) in post.images.slice(0, 3)" 
							:key="imgIndex" 
							:src="img" 
							mode="aspectFill"
							@click.stop="previewImage(post.images, imgIndex)"
						></image>
					</view>
				</view>
				<view class="post-footer">
					<view class="action-item">
						<text class="iconfont icon-like">👍</text>
						<text class="count">{{ post.likes }}</text>
					</view>
					<view class="action-item">
						<text class="iconfont icon-comment">💬</text>
						<text class="count">{{ post.comments }}</text>
					</view>
					<view class="action-item">
						<text class="iconfont icon-share">↗️</text>
						<text class="count">{{ post.shares }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			searchText: '',
			currentCategory: 0,
			categories: [
				{ id: 0, name: '全部' },
				{ id: 1, name: '情感' },
				{ id: 2, name: '美食' },
				{ id: 3, name: '旅行' },
				{ id: 4, name: '时尚' },
				{ id: 5, name: '运动' },
				{ id: 6, name: '科技' }
			],
			posts: [
				{
					id: 1,
					author: {
						name: '小美',
						avatar: '/static/images/avatar1.jpg'
					},
					title: '今天的约会太开心了！',
					content: '和喜欢的人一起去看了电影，吃了美食，漫步在街头，感觉整个世界都在发光...',
					images: [
						'/static/images/post1.jpg',
						'/static/images/post2.jpg',
						'/static/images/post3.jpg'
					],
					createTime: '10分钟前',
					likes: 128,
					comments: 32,
					shares: 16
				},
				{
					id: 2,
					author: {
						name: '小明',
						avatar: '/static/images/avatar2.jpg'
					},
					title: '分享一个约会小技巧',
					content: '第一次约会选择咖啡厅是个不错的选择，环境安静，适合交谈，还能测试对方的品味...',
					images: [
						'/static/images/post4.jpg'
					],
					createTime: '30分钟前',
					likes: 256,
					comments: 64,
					shares: 32
				}
			],
			page: 1,
			loading: false
		}
	},
	methods: {
		createPost() {
			uni.navigateTo({
				url: '/pages/explore/post'
			})
		},
		selectCategory(categoryId) {
			this.currentCategory = categoryId
			this.refreshPosts()
		},
		onRefresh() {
			this.refreshPosts()
		},
		loadMore() {
			if (this.loading) return
			this.loading = true
			// TODO: 加载更多帖子
			setTimeout(() => {
				// 模拟加载更多数据
				this.posts = [...this.posts, ...this.posts]
				this.loading = false
			}, 1000)
		},
		refreshPosts() {
			// TODO: 刷新帖子列表
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		viewPostDetail(postId) {
			uni.navigateTo({
				url: `/pages/explore/detail?id=${postId}`
			})
		},
		previewImage(urls, current) {
			uni.previewImage({
				urls: urls,
				current: urls[current]
			})
		}
	}
}
</script>

<style lang="scss">
.forum-container {
	min-height: 100vh;
	background: #f5f6f7;
	padding-bottom: env(safe-area-inset-bottom);
	
	.search-bar {
		padding: 20rpx;
		background: #fff;
		display: flex;
		align-items: center;
		position: sticky;
		top: 0;
		z-index: 100;
		
		.search-input {
			flex: 1;
			height: 72rpx;
			background: #f5f6f7;
			border-radius: 36rpx;
			display: flex;
			align-items: center;
			padding: 0 30rpx;
			margin-right: 20rpx;
			
			.icon-search {
				font-size: 32rpx;
				color: #999;
				margin-right: 10rpx;
			}
			
			input {
				flex: 1;
				font-size: 28rpx;
			}
		}
		
		.post-btn {
			width: 72rpx;
			height: 72rpx;
			background: #ff6b6b;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			
			.icon-edit {
				font-size: 32rpx;
				color: #fff;
			}
		}
	}
	
	.category-scroll {
		background: #fff;
		padding: 20rpx 0;
		white-space: nowrap;
		
		.category-list {
			padding: 0 20rpx;
			display: inline-flex;
			
			.category-item {
				padding: 12rpx 30rpx;
				font-size: 28rpx;
				color: #666;
				background: #f5f6f7;
				border-radius: 30rpx;
				margin-right: 20rpx;
				transition: all 0.3s;
				
				&.active {
					color: #fff;
					background: #ff6b6b;
				}
			}
		}
	}
	
	.posts-list {
		padding: 20rpx;
		
		.post-item {
			background: #fff;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;
			
			.post-header {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;
				
				.avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					margin-right: 16rpx;
				}
				
				.author-info {
					.author-name {
						font-size: 28rpx;
						font-weight: 500;
						color: #333;
						display: block;
					}
					
					.post-time {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
			
			.post-content {
				.post-title {
					font-size: 32rpx;
					font-weight: 600;
					color: #333;
					margin-bottom: 12rpx;
					display: block;
				}
				
				.post-text {
					font-size: 28rpx;
					color: #666;
					line-height: 1.6;
					margin-bottom: 16rpx;
					display: block;
				}
				
				.post-images {
					display: grid;
					grid-template-columns: repeat(3, 1fr);
					gap: 10rpx;
					
					image {
						width: 100%;
						height: 200rpx;
						border-radius: 12rpx;
					}
				}
			}
			
			.post-footer {
				display: flex;
				padding-top: 16rpx;
				border-top: 2rpx solid #f5f6f7;
				margin-top: 16rpx;
				
				.action-item {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: center;
					
					.iconfont {
						font-size: 36rpx;
						color: #999;
						margin-right: 8rpx;
					}
					
					.count {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}
}
</style>